Plot FAILED predicted images

For turning

In [36]:
import sys
import os.path
import glob

from scipy import misc
import numpy as np
import imageio

from utils import plotting_tools

grading_data_dir_name='sample_evaluation_data'
In [74]:
# For warnings of 'misc.imresize'
if not sys.warnoptions:
    import warnings
    warnings.filterwarnings('ignore', category=DeprecationWarning)
    
def get_failed_im_file_sample(grading_dir_name, subset_name, out_folder_suffix = 'run_1', n_file_names=10):
    '''Get list of failed samples and failed status'''

    im_files = np.array(plotting_tools.get_im_file_sample(
        grading_dir_name, subset_name, out_folder_suffix, n_file_names=None))
    gt_files = im_files[:,1]
    pred_files = im_files[:,2]
    
    n_preds = len(gt_files)
    n_false_neg = 0
    n_false_pos = 0

    failed_indexes = []
    for e, gt_file in enumerate(gt_files):
        gt_mask = imageio.imread(gt_file).clip(0, 1)
        pred_mask = (imageio.imread(pred_files[e]) > 127).astype(np.int)

        if gt_mask.shape[0] != pred_mask.shape[0]:
            gt_mask = misc.imresize(gt_mask, pred_mask.shape)

        if gt_mask[:,:,2].sum() > 3:
            if pred_mask[:,:, 2].sum() > 3:
                pass
            else:
                n_false_neg += 1
                failed_indexes.append(e)
        else:
            if pred_mask[:, :, 2].sum() > 3:
                n_false_pos += 1
                failed_indexes.append(e)
    
    random_sample_indexes = np.random.permutation(failed_indexes)[:n_file_names]
    if 0 < len(random_sample_indexes):
        ims_subset = im_files[random_sample_indexes, 0]
        masks_subset = im_files[random_sample_indexes, 1]
        preds_subset = im_files[random_sample_indexes, 2]
    else:
        ims_subset = np.array([],dtype=str)
        masks_subset = np.array([],dtype=str)
        preds_subset = np.array([],dtype=str)
        
    return list(zip(ims_subset, masks_subset, preds_subset)), n_preds, n_false_pos, n_false_neg
In [79]:
def show_failed_samples(grading_data_dir_name, subset_name, num_of_samples = 100, run_num = 'run_1'):
    """Predicted Samples Viewer"""
    # Count all iamges
    path = os.path.join('..', 'data', grading_data_dir_name)
    ims = np.array(plotting_tools.get_im_files(path, subset_name))
    print('All images: ', len(ims))

    # Show samples
    im_files, n_preds, n_false_pos, n_false_neg = get_failed_im_file_sample(
        grading_data_dir_name, subset_name, run_num, n_file_names=num_of_samples)
    
    print('number of validation samples intersection over the union evaulated on {}'.format(n_preds))
    print('number false positives: {}(P={:.6}), number false negatives: {}(P={:.6})'.\
          format(n_false_pos, n_false_pos/n_preds, n_false_neg, n_false_neg/n_preds))
    print('number failed: {}(P={:.6})'.\
          format(n_false_pos+n_false_neg, (n_false_pos+n_false_neg)/n_preds))
    print()
    
    print('Sample images: ', len(im_files))
    for i in range(len(im_files[:num_of_samples])):
        print(i)
        im_tuple = plotting_tools.load_images(im_files[i])
        plotting_tools.show_images(im_tuple)
        
show_failed_samples(
    grading_data_dir_name, 
    #subset_name='following_images',
    subset_name='patrol_non_targ',
    #subset_name='patrol_with_targ', 
    num_of_samples=100  # None means all samples
)
All images:  270
number of validation samples intersection over the union evaulated on 270
number false positives: 58(P=0.214815), number false negatives: 0(P=0.0)
number failed: 58(P=0.214815)

Sample images:  58
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-79-01eaf127409f> in <module>()
     26     subset_name='patrol_non_targ',
     27     #subset_name='patrol_with_targ',
---> 28     num_of_samples=100  # None means all samples
     29 )

<ipython-input-79-01eaf127409f> in show_failed_samples(grading_data_dir_name, subset_name, num_of_samples, run_num)
     19         print(i)
     20         im_tuple = plotting_tools.load_images(im_files[i])
---> 21         plotting_tools.show_images(im_tuple)
     22 
     23 show_failed_samples(

~\dev\study\udacity-robotics\2018-nd209\project4-follow-me\RoboND-DeepLearning-Project\code\utils\plotting_tools.py in show_images(maybe_ims, x, y)
     52         for i in maybe_ims[1:]:
     53             new_im = np.concatenate((new_im, border, i), axis=1)
---> 54         show(new_im, len(maybe_ims)*x, y)
     55     else:
     56         show(maybe_ims)

~\dev\study\udacity-robotics\2018-nd209\project4-follow-me\RoboND-DeepLearning-Project\code\utils\plotting_tools.py in show(im, x, y)
     43     plt.figure(figsize=(x,y))
     44     plt.imshow(im)
---> 45     plt.show()
     46 
     47 def show_images(maybe_ims, x=4, y=4):

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\matplotlib\pyplot.py in show(*args, **kw)
    251     """
    252     global _show
--> 253     return _show(*args, **kw)
    254 
    255 

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\ipykernel\pylab\backend_inline.py in show(close, block)
     34     try:
     35         for figure_manager in Gcf.get_all_fig_managers():
---> 36             display(figure_manager.canvas.figure)
     37     finally:
     38         show._to_draw = []

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\display.py in display(include, exclude, metadata, transient, display_id, *objs, **kwargs)
    296             publish_display_data(data=obj, metadata=metadata, **kwargs)
    297         else:
--> 298             format_dict, md_dict = format(obj, include=include, exclude=exclude)
    299             if not format_dict:
    300                 # nothing to display (e.g. _ipython_display_ took over)

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\formatters.py in format(self, obj, include, exclude)
    178             md = None
    179             try:
--> 180                 data = formatter(obj)
    181             except:
    182                 # FIXME: log the exception

<decorator-gen-9> in __call__(self, obj)

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\formatters.py in catch_format_error(method, self, *args, **kwargs)
    222     """show traceback on failed format call"""
    223     try:
--> 224         r = method(self, *args, **kwargs)
    225     except NotImplementedError:
    226         # don't warn on NotImplementedErrors

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    239 
    240     if 'png' in formats:
--> 241         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    242     if 'retina' in formats or 'png2x' in formats:
    243         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    123 
    124     bytes_io = BytesIO()
--> 125     fig.canvas.print_figure(bytes_io, **kw)
    126     data = bytes_io.getvalue()
    127     if fmt == 'svg':

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2261                 orientation=orientation,
   2262                 bbox_inches_restore=_bbox_inches_restore,
-> 2263                 **kwargs)
   2264         finally:
   2265             if bbox_inches and restore_bbox:

~\Anaconda3\envs\RoboND-gpu-1.2\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    526             with cbook.open_file_cm(filename_or_obj, "wb") as fh:
    527                 _png.write_png(renderer._renderer, fh,
--> 528                                self.figure.dpi, metadata=metadata)
    529         finally:
    530             renderer.dpi = original_dpi

KeyboardInterrupt: